package core.api;

import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.util.Logger;

public class DFservice {

	private Logger log = Logger.getJADELogger(this.getClass().getName());

	public void registerAgent(String serviceType, Agent agt){
		// Register the controller service in the yellow pages
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(agt.getAID());
		ServiceDescription sd = new ServiceDescription();
		sd.setType(serviceType);
		sd.setName(serviceType);
		dfd.addServices(sd);
		try {
			DFService.register(agt, dfd);
		}
		catch (FIPAException fe) {
			fe.printStackTrace();
		}
	}




	public AID[] getTypeAgents(String serviceType, Agent agt ) {

		AID[] agentsID = null;
		DFAgentDescription template = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		sd.setType(serviceType);
		template.addServices(sd);
		try {
			DFAgentDescription[] result = DFService.search(agt, template); 
			System.out.println("Found the following calculator agents:");
			if (result.length>0){
				agentsID = new AID[result.length];
			}
			for (int i = 0; i < result.length; ++i) {
				agentsID[i] = result[i].getName();
				log.info(agentsID[i].getName());
			}

		}
		catch (FIPAException fe) {
			fe.printStackTrace();
		}
		return agentsID;
	}
}
